﻿using Logistics.EntityFrameworkCore;
using Logistics.Maintains;
using Logistics.Materials;
using Logistics.Model;
using Logistics.Repair;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Maintainss
{
    public class MaintainsAppService : ApplicationService, IMaintainsAppService
    {
        private readonly IRepository<Maintain, int> _RoleRepository;
        private readonly LogisticsDbContext _db;

        public MaintainsAppService(IRepository<Maintain, int> RoleRepository, LogisticsDbContext db)
        {
            _RoleRepository = RoleRepository;
            _db = db;
        }

        public async Task<MaintainsDto> CreateAddAsync(CreateMaintainsDto input)
        {
            var studentDto = new Maintain
            {
                Title = input.Title,
                VehicleId = input.VehicleId,
                Money = input.Money,
                Person = input.Person,
                Project = input.Project,
                Date = input.Date,
                Remark = input.Remark,
                CreateTime = DateTime.Now,
            };
            await _RoleRepository.InsertAsync(studentDto);
            return ObjectMapper.Map<Maintain, MaintainsDto>(studentDto);
        }

        public async Task DeleteAsync(int id)
        {
            await _RoleRepository.DeleteAsync(id);
        }

        public int DeletePdeleteAsync(string ids)
        {
            var a = ids.Split(',');
            foreach (var item in a)
            {
                var list = _db.Maintain.Find(Convert.ToInt32(item));
                _db.Maintain.RemoveRange(list);
            }

            var i = _db.SaveChanges();
            return i;
        }

        public async Task<MaintainsDto> GetAsync(int id)
        {
            var student = await _RoleRepository.GetAsync(id);
            return ObjectMapper.Map<Maintain, MaintainsDto>(student);
        }

        public async Task<PagedResultDto<MaintainsDto>> GetJobAsync(GetMaintainsListDto dto)
        {
            //上下文对象取值
            var rolelist = _db.Maintain.ToList();

            //判断是否为空
            if (!string.IsNullOrEmpty(dto.Name))
            {
                //添加查询
                rolelist = rolelist.Where(m => m.Person.Contains(dto.Name)).ToList();
            }
            //创建分页获取数据对象
            PagedResultDto<MaintainsDto> page = new PagedResultDto<MaintainsDto>();
            //总条数赋值
            page.TotalCount = rolelist.Count;

            //分页
            rolelist = rolelist.Skip(dto.SkipCount).Take(dto.MaxResultCount).ToList();

            //数据赋值
            page.Items = ObjectMapper.Map<List<Maintain>, List<MaintainsDto>>(rolelist);
            //返回对象
            return page;
        }

        public async Task UpdateAsync(int id, CreateMaintainsDto input)
        {
            var receipts = await _RoleRepository.GetAsync(id);

            receipts.Title = input.Title;
            receipts.VehicleId = input.VehicleId;
            receipts.Money = input.Money;
            receipts.Person = input.Person;
            receipts.Project = input.Project;
            receipts.Date = input.Date;
            receipts.Remark = input.Remark;
            receipts.CreateTime = DateTime.Now;

            await _RoleRepository.UpdateAsync(receipts);
        }
    }
}
